<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
		<!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
		<meta name="renderer" content="webkit">
		<!--国产浏览器高速模式-->
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>电力通信管理系统</title>
		<!-- 公共样式 开始 -->
		<link rel="stylesheet" type="text/css" href="../../css/base.css">
		<link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
		<script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<!-- 滚动条插件 -->
		<link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
		<script src="../../framework/jquery-ui-1.10.4.min.js"></script>
		<script src="../../framework/jquery.mousewheel.min.js"></script>
		<script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
		<script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
		<!-- 公共样式 结束 -->
		<script type="text/javascript" src="../../framework/frame.js" ></script>
		<style>
			.layui-form{
				margin-right: 30%;
			}
		</style>

	</head>

	<body>
		<div class="cBody">
				<form id="form" action="" method="post" class="layui-form">
					<div class="layui-form-item">
						<label class="layui-form-label">日期</label>
						<div class="layui-input-block ">
							<input type="text" class="layui-input" placeholder="请选择日期" name="workDate" id="date" lay-filter="workDate" >
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">用户</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" style="margin-left: 0px;" name="userId" id="workleader" lay-filter="workleader" lay-verify="required">
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">班次</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" style="margin-left: 0px;" name="workTime" id="workTime" lay-filter="workTime" lay-verify="required">
								<option value="0" selected>白班</option>
								<option value="1">夜班</option>
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" id="btn" lay-submit lay-filter="submitBut">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>
		</div>
	</body>

<script>
	$(document).ready(function(){
		getUsers("")
		layui.use('laydate', function(){
			var laydate = layui.laydate;

			//执行一个laydate实例
			laydate.render({
				elem: '#date' //指定元素
			});
		});
	});
	layui.use('form', function() {
		var form = layui.form;
		form.verify({
			workDate: function (value, item) { //value：表单的值、item：表单的DOM对象
				if (value == null || value == "") {
					return '请选择日期！';
				}
			}
		});
		//监听提交
		form.on('submit(submitBut)', function (data) {
			var body = data.field;
			console.log(body)
			if (body.workDate == null || body.workDate === "") {
				layer.msg("请选择日期")
				return false;
			}
			$.ajax({
				type: "post",
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				dataType: "json",
				url: "http://localhost:8888/workforce/save",
				contentType: "application/json;charset=UTF-8",//指定消息请求类型
				data: JSON.stringify(body),//将js对象转成json对象
				success: function (data, status, xhr) {
					if (data.code === 0) {
						layer.msg("添加成功");
						$("#form")[0].reset()
						form.render()
					} else {
						layer.msg(data.msg)
					}
					return false
				},
				error: function () {
					layer.msg("添加失败，请稍后再试")
					return false
				}
			});
			return false
		});
	})
	function getUsers(id) {
		var user = JSON.parse(window.sessionStorage.getItem("user"));
		var body = {
			page: 1,
			limit: 0,
			id: user.id
		}
		$.ajax({
			url: 'http://localhost:8888/user/list',
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			type: 'post',
			data: body,//将js对象转成json对象
			success: function (data) {
				console.log(data)
				$.each(data.data, function (index, item) {
					if (id === "") {
						$('#workleader').append(new Option(item.managername, item.id, null, item.id === id));// 下拉菜单里添加元素
					} else {
						$('#workleaderSearch').append(new Option(item.managername, item.id, null, item.id === id));// 下拉菜单里添加元素
					}
				});
				if (id === "") {
					layui.form.render("select");
				}
			}
		})
	}
</script>

</html>
